package com.zcsy.manage.shop.controller.system;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zcsy.commons.constants.ShopConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.dmall.vo.OrderCount;
import com.zcsy.manage.model.dmall.vo.OrderData;
import com.zcsy.manage.model.mall.OrderDetailInfo;
import com.zcsy.manage.service.mall.IOrderDetailInfoService;

/**
 * @Title: ShopReportController.java
 * @Package： com.zcsy.manage.owner.controller.system;
 * @Description: 数据统计控制层
 * @author yangfan 373188993@qq.com
 * @Copyright: 2016 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年7月25日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/shopReport")
public class ShopReportController {

    @Autowired
    private IOrderDetailInfoService orderDetailInfoService;
    
    //初始化Sum 用来存放取出的和
    double Sum = 0;
    
    /**
     * @Description 查询销量7日或30日销量
     * @param timeType
     *            选取时间类型 1：近7天 2：近30天
     * @param shopId
     *            店铺id
     * @return 销量相关数据
     * @author yangfan 373188993@qq.com
     * @date 2016年7月15日
     * @see
     */
    @RequestMapping(value = "salesReport")
    @ResponseBody
    public RequestJson salesReport(String shopId, Integer timeType) {

        RequestJson result = new RequestJson();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("shopId", shopId);
        map.put("timeType", timeType);

        try {
            List<OrderData> salesRecord = orderDetailInfoService.getSalesAmountByShopId(map);
            OrderCount count = orderDetailInfoService.getSalesAmountDataCount(map);

            if(salesRecord.isEmpty()) {
                result.setSuccess(false);
                result.setResultCode(ShopConstants.RESULT_EXCEPTION_FALSE);
                result.setMsg("未查询到相关数据！");
                return result;
            }
            //将销量为空的补零
            SimpleDateFormat  datetime = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            Date now = new Date();
            int days;
            if(timeType == ShopConstants.REPORT_7DAYS){
                days = 7;
            } else {
                days = 30;
            }
            
            //将有销量的日期放入map中
            Map<String,Integer> dateMap = new HashMap<>();
            int j = 0;
            for(OrderData record : salesRecord) {
                dateMap.put(record.getDate(),j);
                Sum = Sum + record.getSalesAmount();
                j++;
            }
            
            //算出平均数
            Sum = Sum/days ;
            java.text.DecimalFormat   df=new   java.text.DecimalFormat("#.##");
            count.setAvg(String.valueOf(df.format(Sum)));
            
            OrderData data = null;
            for (int i = 0; i < days; i++) {
                cal.setTime(now);
                cal.add(cal.DATE, -days+i);
                
                if(!dateMap.containsKey(datetime.format(cal.getTime()) ) ) {
                    data = new OrderData();
                    data.setDate(datetime.format(cal.getTime()));
                    data.setSalesAmount(0);
                    salesRecord.add(i, data);
                } 
            }
            
            if (!salesRecord.isEmpty() && count != null) {
                Map<String, Object> record = new HashMap<String, Object>();
                record.put("salsesRecord", salesRecord);
                record.put("count", count);

                result.setSuccess(true);
                result.setResultCode(ShopConstants.RESULT_CODE_SUCCESS);
                result.setMsg("查询数据成功！");
                result.setObj(record);
            } else {
                result.setSuccess(false);
                result.setResultCode(ShopConstants.RESULT_EXCEPTION_FALSE);
                result.setMsg("查询数据失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("网络异常");
            result.setResultCode(ShopConstants.RESULT_EXCEPTION_FALSE);
            result.setSuccess(false);
        }
        return result;
    }
    
    /**
     * @Description 查询销量7日或30日销售额
     * @param timeType
     *            选取时间类型 1：近7天 2：近30天
     * @param shopId
     *            店铺id
     * @return result销售额相关数据
     * @author yangfan 373188993@qq.com
     * @date 2016年7月15日
     * @see
     */
    @RequestMapping(value = "salesMoney")
    @ResponseBody
    public RequestJson salesMoney(String shopId, Integer timeType) {

        RequestJson result = new RequestJson();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("shopId", shopId);
        map.put("timeType", timeType);

        try {
            List<OrderData> salesRecord = orderDetailInfoService.getSalesMoneyByShopId(map);
            OrderCount count = orderDetailInfoService.getSalesMoneyDataCount(map);

            if(salesRecord.isEmpty()) {
                result.setSuccess(false);
                result.setResultCode(ShopConstants.RESULT_EXCEPTION_FALSE);
                result.setMsg("未查询到相关数据！");
                return result;
            }
            //将销量为空的补零
            SimpleDateFormat  datetime = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            Date now = new Date();
            int days;
            if(timeType == ShopConstants.REPORT_7DAYS){
                days = 7;
            } else {
                days = 30;
            }
            
            //将有销量的日期放入map中
            Map<String,Integer> dateMap = new HashMap<>();
            int j = 0;
            for(OrderData record : salesRecord) {
                dateMap.put(record.getDate(),j);
                Sum = Sum + record.getSalesPrice();
                j++;
            }
            
            //算出平均数
            Sum = Sum/days ;
            java.text.DecimalFormat   df=new   java.text.DecimalFormat("#.##");
            count.setAvg(String.valueOf(df.format(Sum)));
            
            OrderData data = null;
            for (int i = 0; i < days; i++) {
                cal.setTime(now);
                cal.add(cal.DATE, -days+i);
                
                if(!dateMap.containsKey(datetime.format(cal.getTime()) ) ) {
                    data = new OrderData();
                    data.setDate(datetime.format(cal.getTime()));
                    data.setSalesPrice(0);
                    salesRecord.add(i, data);
                } 
            }
            
            if (!salesRecord.isEmpty() && count !=null) {
                Map<String, Object> record = new HashMap<String, Object>();
                record.put("salsesRecord", salesRecord);
                record.put("count", count);

                result.setSuccess(true);
                result.setResultCode(ShopConstants.RESULT_CODE_SUCCESS);
                result.setMsg("查询数据成功！");
                result.setObj(record);
            } else {
                result.setSuccess(false);
                result.setResultCode(ShopConstants.RESULT_EXCEPTION_FALSE);
                result.setMsg("查询数据失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("网络异常");
            result.setResultCode(ShopConstants.RESULT_EXCEPTION_FALSE);
            result.setSuccess(false);
        }
        return result;
    }
    
    /**
     * @Description 查询销量7日或30日订单量
     * @param timeType
     *            选取时间类型 1：近7天 2：近30天
     * @param shopId
     *            店铺id
     * @return result销售额相关数据
     * @author yangfan 373188993@qq.com
     * @date 2016年7月15日
     * @see
     */
    @RequestMapping(value = "orderNum")
    @ResponseBody
    public RequestJson orderNum(String shopId, Integer timeType) {

        RequestJson result = new RequestJson();
        Map<String, Object> map = new HashMap<String, Object>();
        
        try {
            map.put("shopId", shopId);
            map.put("timeType", timeType);

            List<OrderData> salesRecord = orderDetailInfoService.getSalesOrderNumByShopId(map);
            OrderCount count = orderDetailInfoService.getSalesOrderNumDataCount(map);

            if(salesRecord.isEmpty()) {
                result.setSuccess(false);
                result.setResultCode(ShopConstants.RESULT_EXCEPTION_FALSE);
                result.setMsg("未查询到相关数据！");
                return result;
            }
            //将销量为空的补零
            SimpleDateFormat  datetime = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            Date now = new Date();
            int days;
            if(timeType == ShopConstants.REPORT_7DAYS){
                days = 7;
            } else {
                days = 30;
            }
            
            //将有销量的日期放入map中
            Map<String,Integer> dateMap = new HashMap<>();
            int j = 0;
            for(OrderData record : salesRecord) {
                dateMap.put(record.getDate(),j);
                Sum = Sum + record.getOrderNum();
                j++;
            }
            
            //算出平均数
            Sum = Sum/days ;
            java.text.DecimalFormat   df=new   java.text.DecimalFormat("#.##");
            count.setAvg(String.valueOf(df.format(Sum)));   
            
            OrderData data = null;
            for (int i = 0; i < days; i++) {
                cal.setTime(now);
                cal.add(cal.DATE, -days+i);
                
                if(!dateMap.containsKey(datetime.format(cal.getTime()) ) ) {
                    data = new OrderData();
                    data.setDate(datetime.format(cal.getTime()));
                    data.setOrderNum(0);
                    salesRecord.add(i, data);
                } 
            }
            
            if (!salesRecord.isEmpty() && count !=null) {
                Map<String, Object> record = new HashMap<String, Object>();
                record.put("salsesRecord", salesRecord);
                record.put("count", count);

                result.setSuccess(true);
                result.setResultCode(ShopConstants.RESULT_CODE_SUCCESS);
                result.setMsg("查询数据成功！");
                result.setObj(record);
            } else {
                result.setSuccess(false);
                result.setResultCode(ShopConstants.RESULT_EXCEPTION_FALSE);
                result.setMsg("查询数据失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("网络异常");
            result.setResultCode(ShopConstants.RESULT_EXCEPTION_FALSE);
            result.setSuccess(false);
        }
        return result;
    }
}
